/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.globussoft.xml;

/**
 *
 * @author Dj
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;


public class XmlAdvancedStore
{

    private boolean update;
     private boolean virtual_drive;
     private boolean log;
     private boolean preConfBack;
     private boolean advBack;
     private boolean enSupOpen;
     private boolean enSupEncrypt;
     private boolean showOSfile;
     private boolean showRestoreOpt;
     private String projectName="Pomblast";


     Document dom=null;
     public XmlAdvancedStore()
     {
         projectName=SettingsXml.getProjectName();
     }


      public boolean getUpdate() {

            return update;

      }
    public void setUpdate(boolean update) {

            this.update = update;
         }

      public boolean getVirtualDrive() {

            return virtual_drive;

      }

      public void setVirtualDrive(boolean virtual_drive) {

            this.virtual_drive = virtual_drive;

      }

       public void setPreConfigBack(boolean preConfBack) {

            this.preConfBack = preConfBack;
         }

      public boolean setPreConfigBack() {

            return preConfBack;

      }

      public void setAdvBack(boolean advBack) {

            this.advBack = advBack;
         }

      public boolean getAdvBack() {

            return advBack;

      }

       public void setEnSupOpen(boolean enSupOpen) {

            this.enSupOpen = enSupOpen;
         }

      public boolean getEnSupOpen() {

            return enSupOpen;

      }

       public void setEnSupEncrypt(boolean enSupEncrypt) {

            this.enSupEncrypt = enSupEncrypt;
         }

      public boolean getEnSupEncrypt() {

            return enSupEncrypt;

      }

       public void setShowOSfile(boolean showOSfile) {

            this.showOSfile = showOSfile;
         }

      public boolean getShowOSfile() {

            return showOSfile;

      }

       public void setShowRestoreOpt(boolean showRestoreOpt) {

            this.showRestoreOpt = showRestoreOpt;
         }

      public boolean getShowRestoreOpt() {

            return showRestoreOpt;

      }

      public boolean getLog() {

            return log;

      }

      public void setLog(boolean log) {

            this.log = log;

      }


       public  void writeGeneralXml(String filename) throws ParserConfigurationException{

           //create a list to hold the data
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            try {

                  DocumentBuilder db = dbf.newDocumentBuilder();

                  dom = db.newDocument();

                  Element rootElement = dom.createElement("General-Settings");

                 // dom.appendChild(rootElement);

                  Element generalElement = dom.createElement("Advanced");

                  generalElement=createGeneralElement(generalElement,"Automatic-Update",""+update);
                  generalElement=createGeneralElement(generalElement,"Virtual-Drive",""+virtual_drive);
                  generalElement=createGeneralElement(generalElement,"Automatic-Log",""+log);
                  rootElement.appendChild(generalElement);
                  dom.appendChild(rootElement);

                 try{
                  printToFile(dom,filename);
                 }catch(Exception ex){ex.printStackTrace();}



            }catch(ParserConfigurationException pce) {

                //  System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
                  pce.printStackTrace();
                  System.exit(1);

             }


      }

               /**
	 * Helper method which creates a XML element
	 * @param xmlgeneralstore The SetXmlGeneralStore for which we need to create an xml representation
	 * @return XML element snippet representing a SetXmlGeneralStore
	 */

      private Element createGeneralElement(Element generalElement,String property,String value)
      {


          //create Status element and Status text node and attach it to generalElement
            Element statusElement = dom.createElement(property);

            Text statusText = dom.createTextNode(value);

            statusElement.appendChild(statusText);

            generalElement.appendChild(statusElement);


            return generalElement;

      }

       private void printToFile(Document dom,String filename) throws FileNotFoundException, IOException{
           try
            {

                 OutputFormat format = new OutputFormat(dom);
                  format.setIndenting(true);
                  //URL mySource = XmlGeneralStore.class.getProtectionDomain().getCodeSource().getLocation();
                //  File sourceFile = new File(".");
                 boolean success;
                 String    source=System.getProperty("user.home");
                 String dir=source+"\\"+projectName;
                 File sourceFile = new File(dir);
                 if(!sourceFile.exists())
                 {
                     success=new File(dir).mkdir();
                 }


                // attach text file to application directory(.class file)
                  String xmlfile = dir+"\\"+filename;

               //to generate output to console use this serializer

              //XMLSerializer serializer = new XMLSerializer(System.out, format);

             //to generate a file output use fileoutputstream instead of system.out

                  XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(xmlfile)), format);
                  serializer.serialize(dom);

              } catch(IOException ie)
              {
                 ie.printStackTrace();
              }

      }

       //reading the xml file code her
public void ReadGeneralXml(String filename)
{
   // String storegenerralxml[]=new String[10];
  try {
      // URL mySource = XmlGeneralStore.class.getProtectionDomain().getCodeSource().getLocation();
     // File sourceFile = new File(".");
     // attach text file to application directory(.class file)
      //String xmlfile = sourceFile.getAbsolutePath()+"\\"+"generalsettings.xml";
	  boolean success;
      String    source=System.getProperty("user.home");
      String dir=source+"\\"+projectName;
      File fXmlFile = new File(dir+"\\"+filename);
      if(fXmlFile.isFile())
      {
	  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	  Document doc = dBuilder.parse(fXmlFile);
	  doc.getDocumentElement().normalize();

	 // System.out.println("General Settings :" + doc.getDocumentElement().getNodeName());
	  NodeList nList = doc.getElementsByTagName("Advanced");
	//  System.out.println("-----------------------");

	  for (int temp = 0; temp < nList.getLength(); temp++) {

	    Node nNode = nList.item(temp);

	    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

	      Element eElement = (Element) nNode;

//	     storegenerralxml[0]= getTagValue("Status",eElement);
//	     storegenerralxml[1]= getTagValue("Quota_Warning",eElement);
//	     storegenerralxml[2]=getTagValue("Alert_Days",eElement).trim();
         String updatetemp=getTagValue("Automatic-Update",eElement).trim();
         String virtual_drivetemp= getTagValue("Virtual-Drive",eElement);
         if(updatetemp.equals("false"))
             update=false;
         else
             update=true;
          if(virtual_drivetemp.equals("false"))
             virtual_drive=false;
          else
              virtual_drive=true;
        String logtemp=getTagValue("Automatic-Log",eElement);
        if(logtemp.equals("false"))
             log=false;
          else
              log=true;
           

	    }
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
    //return storegenerralxml;
}

private static String getTagValue(String sTag, Element eElement)
 {
	  NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	  Node nValue = (Node) nlList.item(0);

	  return nValue.getNodeValue();

 }

        
}